package org.example.config;



import lombok.extern.slf4j.Slf4j;
import org.example.interceptor.AdminInterceptor;
import org.example.interceptor.AppInterceptor;
import org.example.interceptor.GlobalInterceptor;
import org.example.util.JsonObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @ Author     ：hanxi
 * @ Date       ：Created in 15:41 2024/3/22
 * @ Description：
 */
@Slf4j
@Configuration
public class WebAppConfigurer extends WebMvcConfigurationSupport {


    /**
     * 用户登录权限拦截器
     *
     * @return
     */
    @Bean
    public AppInterceptor appInterceptor() {
        return new AppInterceptor();
    }

    /**
     * 管理员登录权限拦截器
     * @return
     */
    @Bean
    public AdminInterceptor adminInterceptor() {
        return new AdminInterceptor();
    }


    /**
     * 全局拦截器 打点日志
     *
     * @return
     */
    @Bean
    public GlobalInterceptor globalInterceptor() {
        return new GlobalInterceptor();
    }






    /** =======================================  关于json的配置从这开始 ================================ */
    @Bean
    public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter(){
        MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
        JsonObjectMapper jsonObjectMapper = new JsonObjectMapper();
        converter.setObjectMapper(jsonObjectMapper);
        return converter;
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (int i = 0; i<converters.size(); i++){
            if(converters.get(i) instanceof MappingJackson2HttpMessageConverter){
                converters.remove(i);
            }
        }
        converters.add(getMappingJackson2HttpMessageConverter());
       log.info("=======================加载自定义处理器configureMessageConverters====================");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //全局拦截器
        registry.addInterceptor(globalInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/**/upload/batchUploadBase64","/upload/*","/**/fonts/*", "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.gif", "/**/*.jpg", "/**/*.jpeg", "/**/*.html", "/druid/*");

        //用户端接口拦截
        registry.addInterceptor(appInterceptor()).addPathPatterns("/**/**/api/**").excludePathPatterns("/**/manager/app/api/**","/**/auth/app/api/**");

        //管理端接口拦截
        registry.addInterceptor(adminInterceptor()).addPathPatterns("/**/manager/app/api/**","/**/auth/app/api/**").excludePathPatterns("/**/index/app/api/**","/**/member/app/api/**");
    }
}

